package com.enjoyiot.module.system.service.shipin;

import com.enjoyiot.framework.common.pojo.PageResult;
import com.enjoyiot.module.system.controller.admin.shipin.vo.SHPPageReqVO;
import com.enjoyiot.module.system.controller.admin.shipin.vo.SHPSaveReqVO;
import com.enjoyiot.module.system.dal.dataobject.shipin.SHPDO;
import com.enjoyiot.module.system.dal.mysql.shipin.SHPMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.enjoyiot.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;
import static com.enjoyiot.module.system.enums.ErrorCodeConstants.SHP_NOT_EXISTS;

import static com.enjoyiot.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 视频监控 Service 实现类
 *
 * @author system
 */
@Service
@Validated
public class SHPServiceImpl implements SHPService {

    @Resource
    private SHPMapper sHPMapper;

    @Override
    public Integer createSHP(SHPSaveReqVO createReqVO) {
        // 插入
        SHPDO sHP = BeanUtils.toBean(createReqVO, SHPDO.class);
        sHPMapper.insert(sHP);
        // 返回
        return sHP.getId();
    }

    @Override
    public void updateSHP(SHPSaveReqVO updateReqVO) {
        // 校验存在
        validateSHPExists(updateReqVO.getId());
        // 更新
        SHPDO updateObj = BeanUtils.toBean(updateReqVO, SHPDO.class);
        sHPMapper.updateById(updateObj);
    }

    @Override
    public void deleteSHP(Integer id) {
        // 校验存在
        validateSHPExists(id);
        // 删除
        sHPMapper.deleteById(id);
    }

    private void validateSHPExists(Integer id) {
        if (sHPMapper.selectById(id) == null) {
            throw exception(SHP_NOT_EXISTS);
        }
    }

    @Override
    public SHPDO getSHP(Integer id) {
        return sHPMapper.selectById(id);
    }

    @Override
    public PageResult<SHPDO> getSHPPage(SHPPageReqVO pageReqVO) {
        return sHPMapper.selectPage(pageReqVO);
    }

}